#! /usr/bin/python

# Copyright (C) 2012 Zhan Zongru
# Author: Zhan Zongru
# Contact: zhan.zr@gmail.com
# License: New BSD License

import threading

class repeat_timer(threading.Thread):
	"""Repeat Timer Class.
	"""
	def __init__(self, interval, function, iterations=0, args=[], kwargs={}):
		threading.Thread.__init__(self)
		self.interval = interval
		self.function = function
		self.iterations = iterations
		self.args = args
		self.kwargs = kwargs
		self.finished = threading.Event()
 
	def run(self):
		count = 0
		while not self.finished.is_set() and (self.iterations <= 0 or count < self.iterations):
			self.finished.wait(self.interval)
			if not self.finished.is_set():
				self.function(*self.args, **self.kwargs)
				count += 1
 
	def cancel(self):
		self.finished.set()